<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>资金运作凭证管理</title>
</head>
<body>	
	<script type="text/javascript">
	    
		var zjpzGrid;
		$(function() {
			 $("#btnAdd").click(function () { addRow(); }).linkbutton({ iconCls: 'icon-add', plain: true });
			zjpzGrid =$("#zjpzGrid").datagrid({
				url : '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/dataGrid.shtml',
				fit : true,
				border : false,
				pagination : true,
				idField : '凭证ID',
				pageSize : 10,
				pageList : [ 10, 20, 30, 40, 50 ],
				sortName : 'ID',
				sortOrder : 'ASC',
				checkOnSelect : false,
				selectOnCheck : false,
				nowrap : false,
				striped : true,
				rownumbers : true,
				singleSelect : true,
				
				columns : [ [{
					field : 'ID',
					title : '编号',
					width : 150,
					hidden : true 
				},{
					field : 'action',
					title : '操作',
					align:'center',
					width : 150,
					formatter : function(value, row, index)  { 
						
		                        if (row.editing){
		                            var s = '<a href="#" onclick="saveRow(this)">保存</a> ';
		                            var c = '<a href="#" onclick="cancelrow(this)">撤销</a>';
		                            return s+c;
		                        } else {
		                            var e = '<a href="#" onclick="editrow(this)">编辑</a> ';
		                            var d = '<a href="#" onclick="deleterow(this)">删除</a>';
		                            return e+d;
		                        }
		                    }
				},
				{
					field : 'ZJLSH',
					title : '资金流水号',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 150,
					sortable : true
				}, {
					field : 'ZHAIYAO',
					title : '摘要',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 200,
					sortable : true
				}, {
					field : 'JFFSE',
					title : '借方发生额',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 100,
					sortable : true
				}, {
					field : 'DFFSE',
					title : '贷方发生额',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 100,
					sortable : true
				}, {
					field : 'FJDJS',
					title : '附件单据数',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 100,
					sortable : true
				}
				, {
					/* 下拉 */
					field : 'ZTDM',
					title : '账套代码',
					editor: {
                        type: 'combobox',
                        options: {
                            required: true,
                            editable:false,
                            url: '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/getCWZT.shtml',                       
                            valueField: 'ZTDM',
                            textField: 'ZTMC',
                            panelHeight: 100
                        }
                    },
					width : 150,
					sortable : true
				}
				, {
					field : 'HSDW',
					title : '核算单位',
					editor: {
                        type: 'combobox',
                        options: {
                            required: true,
                            editable:false,
                            url: '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/getHSDW.shtml',                       
                            valueField: 'ID',
                            textField: 'TITLE',
                            panelHeight: 100
                        }
                    },
					width : 150,
					sortable : true
				}, 
					{
					field : 'ZJYWLX',
					title : '资金业务类型',
					editor: {
                        type: 'combobox',
                        options: {
                            required: true,
                            editable:false,
                            url: '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/getZJYWLX.shtml',                       
                            valueField: 'ID',
                            textField: 'TITLE',
                            panelHeight: 100,
                            onSelect: function(data){
                            	alert($(this).combobox('getText'));
                            	var row = $('#zjpzGrid').datagrid('getSelected'); 
                            	var rowIndex = $('#zjpzGrid').datagrid('getRowIndex',row);//获取行号 
                            	var target = $('#zjpzGrid').datagrid('getEditor', {'index':rowIndex,'field':'YWLX'}).target; 
                            	target.combobox('clear'); //清除原来的数据 
                            	var url ='${pageContext.request.contextPath}/business/accounting/zJYZPZGL/getYWLX.shtml?title='+$(this).combobox('getText');     
                            	target.combobox('reload', url);//联动下拉列表重载 
                            }, 
                        }
                    },
					width : 100,
					sortable : true
				}
				,{
					field : 'YWLX',
					title : '业务类型',
					editor: {
                        type: 'combobox',
                        options: {
                            required: true,
                            editable:false,
                            valueField: 'ID',
                            textField: 'TITLE',
                            panelHeight: 100
                        }
                    },
					width : 100,
					sortable : true
				}
				, {
					field : 'XSPZH',
					title : '显示凭证号',
					width : 100,
					sortable : true
				}
				, {
					field : 'PZLB',
					title : '凭证类别',
					editor: {
                        type: 'combobox',
                        options: {
                            required: true,
                            editable:false,
                            url: '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/getPZLB.shtml',                       
                            valueField: 'ID',
                            textField: 'TITLE',
                            panelHeight: 100,

                         }
                    },
					width : 100,
					sortable : true
				}
				]], 
				
				
				toolbar : '#toolbar01',
				onLoadSuccess : function() {
					$('#searchForm table').show();
					parent.$.messager.progress('close');
					$('this').datagrid('tooltip');
				},
				onRowContextMenu : function(e, rowIndex, rowData) {
					e.preventDefault();
					$(this).datagrid('unselectAll');
					$(this).datagrid('selectRow', rowIndex);
					$('#menu01').menu('show', {
						left : e.pageX,
						top : e.pageY
					});
				},
				
				
				onClickRow: onClickRow,
				
				onBeforeEdit:function(index,row){
	                row.editing = true;
	                updateActions(index);
	            },
	            onAfterEdit:function(index,row){
	                row.editing = false;
	                updateActions(index);
	            },
	            onCancelEdit:function(index,row){
	                row.editing = false;
	                updateActions(index);
	            },
				
			});
			
			console.info(zjpzGrid);
			zjpzGrid.datagrid('resize');
		});
		/* 改变操作 */
		 function updateActions(index){
		        $('#zjpzGrid').datagrid('updateRow',{
		            index: index,
		            row:{}
		        });
		    }
		
		
		 	function getRowIndex(target){
		        var tr = $(target).closest('tr.datagrid-row');
		        return parseInt(tr.attr('datagrid-row-index'));
		    }
		 	//编辑当前行
		    function editrow(target){
		        $('#zjpzGrid').datagrid('beginEdit', getRowIndex(target));
		    }
		 	
		    /* //删除当前行
		    function deleterow(target){
		        $.messager.confirm('提示','确定执行该操作?',function(r){
		            if (r){
		                $('#zjpzGrid').datagrid('deleteRow', getRowIndex(target));
		            }
		        });
		    } */
		    

		    /* 撤销 */
		    function cancelrow(target){
		        $('#zjpzGrid').datagrid('cancelEdit', getRowIndex(target));
		    }
		
		// 判断时候存在编辑中的行
		var editIndex = null;
		function endEditing() {
		    if (editIndex == null) { return true }
		    if ($('#zjpzGrid').datagrid('validateRow', editIndex)) {
		        $('#zjpzGrid').datagrid('endEdit', editIndex);
		        editIndex = null;
		        return true;
		    } else {
		        return false;
		    }
		}
		
		// datagrid行点击事件
		function onClickRow(index, row) {
		    if (editIndex != index) {
		        if (endEditing()) {
		            $("#zjpzGrid").datagrid("selectRow", index).datagrid("beginEdit", index);
		            editIndex = index;
		        } else {
		            $("#zjpzGrid").datagrid("selectRow", editIndex);
		        }
		    }
		}
		
		 // 添加一行
		function addRow() {
		    if (endEditing()) {
		      //  $("#zjpzGrid").datagrid("appendRow");
		      	$("#zjpzGrid").datagrid("appendRow", {
		             /* name: "",
		            remark: "",
		            state: "0"  */
		        }); 
		        editIndex = $("#zjpzGrid").datagrid("getRows").length - 1;
		        $("#zjpzGrid").datagrid("selectRow", editIndex).datagrid("beginEdit", editIndex);
		    }
		}
	 
	    /* 保存当前行数据 */
	    function saveRow(target){
	    	
	        $('#zjpzGrid').datagrid('endEdit', getRowIndex(target));
			$.ajax({  
	            type : "post",  
	            async : false,  
	            url : '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/add.shtml?jsonString='+JSON.stringify($('#zjpzGrid').datagrid("getSelected")), 
	            success : function(result) {
	            	var obj = JSON.parse(result);
	                if(obj.success){  
						zjpzGrid.datagrid('reload');
	        			parent.$.modalDialog({
	        				title : '记账凭证',
	        				width : 880,
	        				height : 500,
	        				href : '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/addPage.shtml'
	        			});
	                }else{
	                	alert(obj.msg);
	                	$("#zjpzGrid").datagrid('reload');
	                }
	            }  
	        })  
	    }
 
		
		function deleterow(target) {
			parent.$.messager.confirm('询问', '您是否要删除当前选择数据？', function(b) {
				var id = ($('#zjpzGrid').datagrid("getSelected").ID);
				if (b && id != null) {
					$.post('${pageContext.request.contextPath}/business/accounting/zJYZPZGL/delete.shtml?id='+id, 
						function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
						}else{
							parent.$.messager.alert('提示', result.msg, 'info');
						}
						zjpzGrid.datagrid('reload');
						parent.$.messager.progress('close');
					}, 'JSON');
				}else{
					$('#zjpzGrid').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);
				}
			});
		}

		function searchFun() {
			zjpzGrid.datagrid('load', $.serializeObject($('#searchForm')));
		}
		function cleanFun() {
			$('#searchForm input').val('');
			$('#searchForm #b').val('true');
			zjpzGrid.datagrid('load', {});
		}
		
	</script>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',title:'',border:false" style="height: 100px; overflow: hidden;" >
			<form id="searchForm" action="/business/accounting/zJYZPZGL/dataGrid.shtml" method="post">
				<table class="form_view_border" bordercolordark="#FFFFFF" bordercolorlight="#45b97c" border="px" cellpadding="0" cellspacing="0" style="">
					<tr>
						<th class="panel-header"><nobr>核算单位名称</nobr></th>
						<td><select  style="width: 156px;height: 27px;"
									class="form_view_input combo easyui-combobox"
									name="HSDW"
									data-options="editable: false">
								<option value="">--请选择--</option>
								 <c:forEach items="${requestScope.hsdw }" var="p">
                                 	<option value="${p.ID }">${p.TITLE }</option>
                               	</c:forEach>
							</select>
						</td>
						<th class="panel-header"><nobr>财务账套</nobr></th>
						<td><select  style="width: 156px;height: 27px;"
									class="form_view_input combo easyui-combobox"
									name="CWZT"
									data-options="editable: false">
								<option value="">--请选择--</option>
								<c:forEach items="${requestScope.cwzt }" var="p">
                                 	<option value="${p.ZTDM }">${p.ZTMC }</option>
                               	</c:forEach>
							</select>
						</td>
						<th class="panel-header"><nobr>记账日期</nobr></th>
						<td>
							<input style="width: 156px;height: 27px;" class="easyui-datebox"
							data-options="editable: false"
							name="JZRQ_EQ_DATE_yyyyMMdd" id="dd"
							placeholder="记账日期" 
							class="span2" />
						</td>
						<td><input id="b" name="b" type="hidden" value="true"/></td>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false">
			<!-- <form id="addForm" action="/add.shtml" method="post"> -->
				<table id="zjpzGrid"></table>
			 <!-- </form>  -->
		</div>
	</div>
	<div id="toolbar01" style="display: none;">
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'brick_add',plain:true" id="btnAdd" >新增</a>
		    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'brick_select',plain:true" onclick="searchFun();">查询</a>
		    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="cleanFun();">重置</a>
		    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="lsxj();">删除</a>
	
	</div>
	<div id="menu01" class="easyui-menu" style="width: 120px; display: none;">
			<!-- <div onclick="addFun();" data-options="iconCls:'pencil_add'">增加</div>
			<div onclick="deleteFun();" data-options="iconCls:'pencil_delete'">删除</div>
			<div onclick="editFun();" data-options="iconCls:'pencil'">编辑</div> -->
	</div> 
	<script type="text/javascript">
		
	</script>
</body>
</html>